define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'post/comment/index',
                    add_url: 'post/comment/add',
                    edit_url: 'post/comment/edit',
                    del_url: 'post/comment/del',
                    comment_url:'post/comment/comment',
                    multi_url: 'post/post/multi',
                    table: 'post_comment',
                }
            });

            var table = $("#table");
            Fast.config.openArea = ['100%','100%'];

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'comment.id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: false },
                        { field: 'content', title: __('content'), operate: 'LIKE' },
                        { field: 'level', title: __('level'), operate: 'LIKE' },
                        { field: 'business.nickname', title: __('busid'), operate: 'LIKE' },
                        { field: 'pid', title: __('pid'), operate: 'LIKE' },
                        { field: 'like_text', title: __('like'), operate: 'LIKE' },
                        { field: 'postid', title: __('postid'), operate: 'LIKE' },
                        // { field: 'status', title: __('status'), operate: 'LIKE' },
                        { field: 'status', title: __('status'), searchList: { "0": __('未采纳'), "1": __('已采纳') }, formatter: Table.api.formatter.flag },
                        { field: 'createtime', title: __('createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                        }
                    ]
                ],
                // escape:false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $('#pid_text').click(function (){
                Backend.api.open("post/comment/comment", "上级评论", {
                    area: ['80%', '80%'],
                    callback: function (data) {
                        $('#pid_text').val(data.business.nickname)
                        $('#pid').val(data.id)
                    }
                })
            })
            $('#postid_text').click(function (){
                Backend.api.open("post/comment/post", "帖子", {
                    area: ['80%', '80%'],
                    callback: function (data) {
                        $('#postid_text').val(data.title)
                        $('#postid').val(data.id)
                    }
                })
            })
            
            $('#busid_text').click(function (){
                Backend.api.open("post/post/business", "客户", {
                    area: ['80%', '80%'],
                    callback: function (data) {
                        $('#busid_text').val(data.nickname)
                        $('#busid').val(data.id)
                    }
                })
            })
            Controller.api.bindevent();
        },
        comment:function()
        {
            Table.api.init({
            });
            // console.log(business.id);
            var CommentTable = $("#table");
            CommentTable.bootstrapTable({
                url: 'post/comment/index',
                pk: 'id',
                sortName: 'comment.id',
                search: false,
                // 关闭导出
                showExport: false,
                // 关闭通用搜索
                commonSearch: false,
                // 关闭显示的列
                showColumns: false,
                // 关闭切换显示
                showToggle: false,
                columns: [
                    [
                        // {checkbox: true},
                        { field: 'id', title: __('Id'), searchable: true },
                        { field: 'id', title: __('Id'), sortable: false },
                        { field: 'content', title: __('content'), operate: 'LIKE' },
                        { field: 'level', title: __('level'), operate: 'LIKE' },
                        { field: 'business.nickname', title: __('busid'), operate: 'LIKE' },
                        { field: 'pid', title: __('pid'), operate: 'LIKE' },
                        { field: 'like_text', title: __('like'), operate: 'LIKE' },
                        { field: 'postid', title: __('postid'), operate: 'LIKE' },
                        // { field: 'status', title: __('status'), operate: 'LIKE' },
                        { field: 'status', title: __('status'), searchList: { "0": __('未采纳'), "1": __('已采纳') }, formatter: Table.api.formatter.flag },
                        { field: 'createtime', title: __('createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: CommentTable,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'chapter', text: '选择',
                                    title: '选择',
                                    classname: 'btn btn-primary btn-xs Comment',
                                },
                            ]
                        }
                    ]
                ]
            });
            // 点击选择
            $('#table').on('click', '.Comment', function () {
                let index = $(this).parents('tr').data('index')
                let data = Table.api.getrowdata(CommentTable, index);
                // console.log(data);
                Backend.api.close(data)
            })
            // 为表格绑定事件
            Table.api.bindevent(CommentTable);
        },
        business:function()
        {
            Table.api.init({
            });
            // console.log(business.id);
            var businessTable = $("#table");
            businessTable.bootstrapTable({
                url: 'post/post/business',
                pk: 'id',
                sortName: 'business.id',
                search: false,
                // 关闭导出
                showExport: false,
                // 关闭通用搜索
                commonSearch: false,
                // 关闭显示的列
                showColumns: false,
                // 关闭切换显示
                showToggle: false,
                columns: [
                    [
                        // {checkbox: true},
                        { field: 'id', title: __('Id'), searchable: true },
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'avatar_text', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        {
                            field: 'gender', title: __('Gender'), searchList: { '0': __('Gender 0'), '1': __('Gender 1'), '2': __('Gender 2') },
                            operate: 'FIND_IN_SET',
                            formatter: Table.api.formatter.normal
                        },
                        { field: 'source.name', title: __('Source'), operate: 'LIKE' },
                        { field: 'email', title: __('Email'), operate: 'LIKE' },
                        // 日期搜索格式化
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: businessTable,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'chapter', text: '选择',
                                    title: '选择',
                                    classname: 'btn btn-primary btn-xs business',
                                },
                            ]
                        }
                    ]
                ]
            });
            // 点击选择
            $('#table').on('click', '.business', function () {
                let index = $(this).parents('tr').data('index')
                let data = Table.api.getrowdata(businessTable, index);
                // console.log(data);
                Backend.api.close(data)
            })
            // 为表格绑定事件
            Table.api.bindevent(businessTable);
        },
        post:function()
        {
            Table.api.init({
            });
            // console.log(business.id);
            var PostTable = $("#table");
            PostTable.bootstrapTable({
                url: 'post/post/index',
                pk: 'id',
                sortName: 'business.id',
                search: false,
                // 关闭导出
                showExport: false,
                // 关闭通用搜索
                commonSearch: false,
                // 关闭显示的列
                showColumns: false,
                // 关闭切换显示
                showToggle: false,
                columns: [
                    [
                        // {checkbox: true},
                        { field: 'id', title: __('Id'), searchable: true },
                        { field: 'title', title: __('title'), operate: 'LIKE' },
                        // { field: 'content', title: __('content'), operate: 'LIKE',},
                        { field: 'category.name', title: __('cateid'), },
                        { field: 'point', title: __('point'), operate: false },
                        { field: 'business.nickname', title: __('Postbusid'), operate: 'LIKE' },
                        { field: 'accepted.nickname', title: __('accept'), searchable: false },
                        { field: 'status', title: __('PostStatus'), searchList: { "0": __('未采纳'), "1": __('已采纳') }, formatter: Table.api.formatter.flag },
                        { field: 'createtime', title: __('Postcreatetime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange'},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: PostTable,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'chapter', text: '选择',
                                    title: '选择',
                                    classname: 'btn btn-primary btn-xs Post',
                                },
                            ]
                        }
                    ]
                ]
            });
            // 点击选择
            $('#table').on('click', '.Post', function () {
                let index = $(this).parents('tr').data('index')
                let data = Table.api.getrowdata(PostTable, index);
                // console.log(data);
                Backend.api.close(data)
            })
            // 为表格绑定事件
            Table.api.bindevent(PostTable);
        },
        edit: function () {
            $('#pid_text').click(function (){
                Backend.api.open("post/comment/comment", "上级评论", {
                    area: ['80%', '80%'],
                    callback: function (data) {
                        $('#pid_text').val(data.business.nickname)
                        $('#pid').val(data.id)
                    }
                })
            })
            $('#postid_text').click(function (){
                Backend.api.open("post/comment/post", "帖子", {
                    area: ['80%', '80%'],
                    callback: function (data) {
                        $('#postid_text').val(data.title)
                        $('#postid').val(data.id)
                    }
                })
            })
            
            $('#busid_text').click(function (){
                Backend.api.open("post/post/business", "客户", {
                    area: ['80%', '80%'],
                    callback: function (data) {
                        $('#busid_text').val(data.nickname)
                        $('#busid').val(data.id)
                    }
                })
            })
            Controller.api.bindevent();
        },
        del: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});